package com.fairytask.engine.components.Scroll
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.utils.draw.DrawAPI;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * 滚动面板
	 * 使用方法
	 * 
	 * @author hanjy
	 */
	public class ScrollPanel extends BaseComponent
	{
		private var verticalBar:ScrollBar;
		private var horizontalBar:ScrollBar;
		
		/**目标显示对象*/
		private var scrollView:DisplayObjectContainer;
		/**可视区域*/
		private var _viewSize:Rectangle;
		/**新容器*/
		private var container:Sprite;
		private var _maxScrollX:uint;
		private var _maxScrollY:uint;
		
		private var _cTcX:int = 	0;
		private var _cTcY:int	=	0;
		
		private var _scroll		:Number;
		
		private var _scrollval		:Number;
		
		private var isVertical:Boolean = false;
		private var isHorizontal:Boolean = false;
		
		
		/**
		 * 
		 * @param viewSize
		 * @param target
		 * @param _skin
		 * @param showScroll
		 */
		public function ScrollPanel(viewSize:Rectangle,target:DisplayObjectContainer,_skin:ScrollSkin=null,showScroll:Boolean=true)
		{
			super();
			
			if(_skin==null)
			{
				_skin	=	new ScrollSkin("vScrol_1","vScrol_2");
			}
			
			container = new Sprite();
			scrollView=	new Sprite();
			scrollView.addChild(target);
			_maxScrollX = target.width;
			_maxScrollY = target.height;
			container.x = scrollView.x;
			container.y = scrollView.y;
			_viewSize = viewSize;
			
			addChild(scrollView);
			addChild(container);
			
			/**加入滚动条*/
			verticalBar = new ScrollBar(ScrollBar.VERTICAL,0,_viewSize.height,30,_skin);
			container.addChild(verticalBar);
			verticalBar.visible = false;
			
			horizontalBar = new ScrollBar(ScrollBar.HORIZONTAL,0,_viewSize.width,30,_skin);
			container.addChild(horizontalBar);
			horizontalBar.visible	=	false;
			drawScrollPanel();
		}
		/**
		 * 更新scrollPanel 
		 * 
		 */
		public function update(barVal:Number=0):void
		{
			_maxScrollX = DrawAPI.getTrueRect(scrollView).width;
			_maxScrollY = DrawAPI.getTrueRect(scrollView).height;
			drawScrollPanel();
			
			_scrollval	=	barVal;
			if(barVal!=-1)
			{
				if(isVertical)
				{
					verticalBar.scroll	=	barVal;
				}
				if(isHorizontal)
				{
					horizontalBar.scroll=	barVal;
				}
			}
		}	
		
		public function removeScrollBar():void
		{
			scrollView.scrollRect  =	null;
			horizontalBar.visible = false;
			verticalBar.visible	=	false;
		}
		
		public function hideScrollBar():void
		{
			horizontalBar.visible = false;
			verticalBar.visible	=	false;
		}
		
		public function showScrollBar(isTxt:Boolean	=	false):void
		{
			var trueViewSize:Rectangle	=	DrawAPI.getTrueRect(scrollView);
			if(trueViewSize.height>_viewSize.height)
			{
				verticalBar.visible	=	true;
			}
			if(trueViewSize.width>_viewSize.width)
			{
				if(!isTxt)
				{
					horizontalBar.visible = true;
				}
			}
		}
				
		/**
		 * 绘制滚动面板 
		 * 
		 */
		private function drawScrollPanel():void
		{
			var barSize:int;
			
			var trueViewSize:Rectangle	=	DrawAPI.getTrueRect(scrollView);	//显示范围
			
			if(trueViewSize.height>_viewSize.height)		//Rectangle区域
			{
				barSize	=	(_viewSize.height-32)*(_viewSize.height-32)/trueViewSize.height;
				verticalBar.update(_viewSize.height,barSize);
				verticalBar.x = _viewSize.width-16;
				_maxScrollX += 16;
				//scrollView.x	-=	16;
				if(!verticalBar.hasEventListener(ScrollEvent.UPDATE_AREA))
				{
					verticalBar.addEventListener(ScrollEvent.UPDATE_AREA, checkBar);
				}
				isVertical = true;
				verticalBar.visible	=	true;
				scrollView.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
				verticalBar.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			}else
			{
				if(scrollView.hasEventListener(MouseEvent.MOUSE_WHEEL))
				{
					scrollView.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
				}
				verticalBar.visible	=	false;
			}
			if(trueViewSize.width>_viewSize.width)
			{
				barSize	=	(_viewSize.width-32)*(_viewSize.width-32)/trueViewSize.width;
				horizontalBar.update(_viewSize.width,barSize);
				horizontalBar.y = _viewSize.height;
				if(!horizontalBar.hasEventListener(ScrollEvent.UPDATE_AREA))
				{
					horizontalBar.addEventListener(ScrollEvent.UPDATE_AREA, checkBar);
				}
				isHorizontal = true;
				horizontalBar.visible	=	true;
			}
			else
			{
				horizontalBar.visible	=	false;
			}
			if(_scrollval!=-1)
			{
				checkBar(null);
			}
			
		}
		
		private function onMouseWheel(e:MouseEvent):void
		{
			verticalBar.onMouseWheel(e);
		}
		/**
		 * 更新滚动区域 
		 * @param e
		 * 
		 */
		private function checkBar(e:ScrollEvent):void
		{
			if(e!=null)
			{
				var barObj:Object = e.obj;
				var tcX		:uint	=	barObj.barX - 16;
				var tcY		:uint	=	barObj.barY - 16;
				if(barObj.type == ScrollBar.VERTICAL)
				{
					_cTcY 	=	(_maxScrollY-_viewSize.height)*tcY/(_viewSize.height-barObj.barSize-32);
					_cTcY	=	_cTcY<0?0:_cTcY;
				}
				else
				{
					_cTcX = (_maxScrollX-_viewSize.width)*tcX/(_viewSize.width-barObj.barSize-32);
					_cTcX	=	_cTcX<0?0:_cTcX;
				}
				scrollView.scrollRect	=	new Rectangle(_cTcX,_cTcY,_viewSize.width,_viewSize.height);
			}else{
				scrollView.scrollRect	=	new Rectangle(0,0,_viewSize.width,_viewSize.height);
			}
			dispatchEvent(new ScrollEvent(ScrollEvent.UPDATE_AREA));
		}

		public function get cTcX():uint
		{
			return _cTcX;
		}

		public function set cTcX(value:uint):void
		{
			_cTcX = value;
		}

		public function get cTcY():uint
		{
			return _cTcY;
		}

		public function set cTcY(value:uint):void
		{
			_cTcY = value;
		}
		
		public function remove():void
		{
			scrollView.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			if(this.parent != null)
			{
				this.parent.removeChild(this);
			}			
		}
		
		/**
		 * 重设大小
		 * @param _reSize
		 * 
		 */
		public function resize(_reSize:Rectangle):void
		{
			_viewSize = _reSize;
//			drawScrollPanel();
			update();
		}
	}
}